package cn.jiedh.app.meiweidk.loan;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.tool.L;
import cn.jiedh.app.meiweidk.Utils.tool.SPUtils;
import cn.jiedh.app.meiweidk.Utils.tool.T;
import cn.jiedh.app.meiweidk.Utils.tool.UrlUtils;
import cn.jiedh.app.meiweidk.base.BaseActivity;
import cn.jiedh.app.meiweidk.buried.BuriedBean;

/**
 * @name：
 * @author：司马林
 * @phone：18423134135
 * @createTime：2017/10/23
 */

public class AddCommentsActivity extends BaseActivity implements TextWatcher {
    @BindView(R.id.bar_tv_title)
    TextView title;
    @BindView(R.id.comment_edt)
    EditText commentEdt;
    @BindView(R.id.comment_text_length)
    TextView commentTextLengthTv;
    @BindView(R.id.loan_title)
    TextView loanTitleTv;
    @BindView(R.id.loan_advert)
    TextView loanAdvertTv;
    @BindView(R.id.loan_icon)
    ImageView loanIconImg;
    @BindView(R.id.interest_rate_satisfaction)
    RatingBar interestRateRatingBar;
    @BindView(R.id.loan_limit_satisfaction)
    RatingBar loanLimitRatingBar;
    @BindView(R.id.lending_speed_satisfaction)
    RatingBar lendingSpeedRatingBar;

    private int productId;
    private String iconUrl;
    private String loanTitle;
    private String loanAdvert;


    @Override
    protected int getLayoutView() {
        return R.layout.activity_add_comments;
    }

    @Override
    protected void initView(View view) {
        title.setText("新增评价");
        if (getIntent().getExtras() != null) {
            productId = getIntent().getExtras().getInt("productId");
            iconUrl = getIntent().getExtras().getString("iconUrl");
            loanTitle = getIntent().getExtras().getString("loanTitle");
            loanAdvert = getIntent().getExtras().getString("loanAdvert");
        }
        loanTitleTv.setText(loanTitle);
        Glide.with(this).load(iconUrl).into(loanIconImg);
        loanAdvertTv.setText(loanAdvert);

        commentEdt.addTextChangedListener(this);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.apply})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.apply:
                List<BuriedBean> buriedList = new ArrayList<>();
                buriedList.add(new BuriedBean("click","新增评价-提交按钮","0",productId+"",loanTitle,"新增评价","false","","","","","","","","","",""));
                SPUtils.saveBuriedList(buriedList);
                addComment();
                break;
            default:
                break;
        }
    }

    private void addComment() {
        int interestRate = (int) interestRateRatingBar.getRating();
        int loanLimit = (int) loanLimitRatingBar.getRating();
        int lendingSpeed = (int) lendingSpeedRatingBar.getRating();
        String comment = commentEdt.getText().toString();
        if (interestRate == 0) {
            T.showShort("您还没对产品利率满意度评分");
            return;
        }
        if (loanLimit == 0) {
            T.showShort("您还没对贷款额度满意度评分");
            return;
        }
        if (lendingSpeed == 0) {
            T.showShort("您还没对放款速度满意度评分");
            return;
        }
        if (TextUtils.isEmpty(comment)) {
            T.showShort("请您输入评价内容");
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("productId", productId);
        map.put("rateScore", interestRate);
        map.put("quotaScore", loanLimit);
        map.put("speedScore", lendingSpeed);
        map.put("content", comment);

        showLoadingDialog();
        request(this, UrlUtils.ADDCOMMENT, map, false, response -> {
            dismissLoadingDialog();
            if (0 == response.getCode()) {
                finish();
            }
            T.showShort(response.getMessage());
        }, (error, msg) -> {
            L.e("添加评论", msg);
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        commentTextLengthTv.setText(commentEdt.getText().length() + "/140");
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
